<template>
  <div class="register-container">
    <div class="register-box">
      <h1>注册账号</h1>
      <p>创建您的气象站管理系统账号</p>
      
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            id="username"
            v-model="registerForm.username" 
            type="text" 
            placeholder="请输入用户名" 
            class="input-field"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            id="password"
            v-model="registerForm.password" 
            type="password" 
            placeholder="请输入密码" 
            class="input-field"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input 
            id="confirmPassword"
            v-model="registerForm.confirmPassword" 
            type="password" 
            placeholder="请再次输入密码" 
            class="input-field"
            required
          />
        </div>
        
        <div class="form-actions">
          <button 
            type="submit" 
            :disabled="loading"
            class="register-btn"
          >
            {{ loading ? '注册中...' : '注册' }}
          </button>
        </div>
      </form>
      
      <div class="login-link">
        <p>已有账号？<router-link to="/login">立即登录</router-link></p>
      </div>
      
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { register } from '@/api/auth'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const error = ref('')

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})

const handleRegister = async () => {
  // 表单验证
  if (!registerForm.username || !registerForm.password || !registerForm.confirmPassword) {
    error.value = '请填写所有必填项'
    return
  }
  
  if (registerForm.password !== registerForm.confirmPassword) {
    error.value = '两次输入的密码不一致'
    return
  }
  
  if (registerForm.password.length < 5) {
    error.value = '密码长度不能少于5位'
    return
  }
  
  error.value = ''
  loading.value = true
  
  try {
    // 调用注册API
    await register({
      username: registerForm.username,
      password: registerForm.password
    })
    
    ElMessage.success('注册成功，请登录')
    
    // 跳转到登录页
    router.push('/login')
  } catch (err) {
    console.error('注册失败:', err)
    error.value = err.message || '注册失败，请重试'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.register-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.register-box {
  background: white;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  text-align: center;
}

h1 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

p {
  margin: 0 0 30px 0;
  font-size: 14px;
  color: #666;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.input-field {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  box-sizing: border-box;
}

.input-field:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.form-actions {
  margin-bottom: 20px;
}

.register-btn {
  width: 100%;
  padding: 12px;
  background: #409eff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
}

.register-btn:hover {
  background: #337ecc;
}

.register-btn:disabled {
  background: #a0cfff;
  cursor: not-allowed;
}

.login-link {
  margin-top: 16px;
  font-size: 14px;
}

.login-link a {
  color: #409eff;
  text-decoration: none;
  font-weight: 500;
}

.login-link a:hover {
  text-decoration: underline;
}

.error-message {
  margin-top: 16px;
  padding: 10px;
  background-color: #fef0f0;
  color: #f56c6c;
  border-radius: 8px;
  font-size: 14px;
}
</style> 